package com.zw.mes.module.shipments.controller;


import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.shipments.InstShipmentsDetail;
import com.zw.mes.entity.shipments.InstShipmentsList;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.shipments.repository.InstShipmentsRepository;
import com.zw.mes.module.shipments.repository.PlanSheetRepository;
import com.zw.mes.module.shipments.service.InstShipmentsPlanService;
import com.zw.mes.module.shipments.service.InstShipmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;

import static com.zw.mes.constant.Constant.GROUP_SPECTYPE;


/**
 * @Description: 创建发货清单
 * <p>
 * 权限：
 * @Author: liqingyu
 * @Date: 2020/11/10 08:58
 */
@Controller
@RequestMapping("/shipments")
@SuppressWarnings("all")
public class InstShipmentsController extends BaseController {


    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private InstShipmentsPlanService instShipmentsPlanService;
    @Autowired
    private PlanSheetRepository planSheetRepository;
    @Autowired
    private InstShipmentsService instShipmentsService;
    @Autowired
    private InstShipmentsRepository instShipmentsRepository;


    @GetMapping
    public String page(Model model) {
        return "shipments/InstShipments/page";
    }

    /**
     * 清单查询
     * @param model
     * @return
     */
    @GetMapping("/comit")
    public String comit(Model model) {
        return "shipments/InstShipmentsSelect/pageComit";
    }
    /**
     * 清单查询
     * @param model
     * @return
     */
    @GetMapping("/query")
    public String query(Model model) {
        return "shipments/InstShipmentsSelect/page";
    }
    /**
     * 发货清单主单
     */
    @ResponseBody
    @PostMapping("/shipmentsList")
    public JsonResult shipmentsList(InstShipmentsList instShipmentsList) {
        startPage();
        List<InstShipmentsList> lists = instShipmentsService.selectInstShipmentsList(instShipmentsList);
        return jsonResult(lists);
    }
    /**
     * 保存发货清单
     */
    @ResponseBody
    @PostMapping("/addShipmentsDetail")
    public JsonResult addShipmentsDetail(InstShipmentsList instShipmentsList, String[] drawName, String[] drawNo, String[] prodName, Integer[] drawNum, String[] unit, String[] remark) {
        int a = instShipmentsService.saveAll(instShipmentsList, drawName, drawNo, prodName, drawNum, unit, remark);
        return jsonResult(1);
    }
    /**
     * 跳转修改发货清单
     * @param model
     * @return
     */
    @GetMapping("/listEdit/{id}")
    public String listEdit(Model model,@PathVariable("id") Integer id) {
        InstShipmentsList instShipmentsList = instShipmentsRepository.getOne(id);
        InstShipmentsDetail instShipmentsDetail = new InstShipmentsDetail();
        instShipmentsDetail.setSheetId(id);
        List<InstShipmentsDetail> instShipmentsDetails = instShipmentsService.selectInstShipmentsDetail(instShipmentsDetail);
        model.addAttribute("instShipmentsList", instShipmentsList);
        model.addAttribute("instShipmentsDetails", instShipmentsDetails);
        return "shipments/InstShipmentsSelect/pageEdit";
    }
    /**
     * 跳转修改发货清单
     * @param model
     * @return
     */
    @GetMapping("/listSelect/{id}")
    public String listSelect(Model model,@PathVariable("id") Integer id) {
        InstShipmentsList instShipmentsList = instShipmentsRepository.getOne(id);
        InstShipmentsDetail instShipmentsDetail = new InstShipmentsDetail();
        instShipmentsDetail.setSheetId(id);
        List<InstShipmentsDetail> instShipmentsDetails = instShipmentsService.selectInstShipmentsDetail(instShipmentsDetail);
        model.addAttribute("instShipmentsList", instShipmentsList);
        model.addAttribute("instShipmentsDetails", instShipmentsDetails);
        return "shipments/InstShipmentsSelect/pageSelect";
    }
    /**
     * 保存修改发货清单
     */
    @ResponseBody
    @PostMapping("/listDetailEdit")
    public JsonResult listDetailEdit(InstShipmentsList instShipmentsList, String[] drawName, String[] drawNo, String[] prodName, Integer[] drawNum, String[] unit, String[] remark) {
        int a = instShipmentsService.saveAllEdit(instShipmentsList, drawName, drawNo, prodName, drawNum, unit, remark);
        return jsonResult(1);
    }
    /**
     * 删除发货清单
     */
    @ResponseBody
    @PostMapping("/del")
    public JsonResult del(InstShipmentsList instShipmentsList) {
        int a = instShipmentsService.del(instShipmentsList);
        return jsonResult(1);
    }
    /**
     * 修改提交状态发货清单
     */
    @ResponseBody
    @PostMapping("/updateStaus")
    public JsonResult updateStaus(InstShipmentsList instShipmentsList) {
        int a = instShipmentsService.updateStaus(instShipmentsList);
        return jsonResult(1);
    }
    /**
     * 修改提交状态发货清单
     */
    @ResponseBody
    @PostMapping("/submit")
    public JsonResult submit(InstShipmentsList instShipmentsList) {
        int a = instShipmentsService.submit(instShipmentsList);
        return jsonResult(1);
    }
}
